package com.sendpo.zjx.sendpo.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.sendpo.zjx.sendpo.R;
import com.sendpo.zjx.sendpo.domain.UserDomain;
import com.sendpo.zjx.sendpo.net.HttpCode;
import com.sendpo.zjx.sendpo.net.HttpUrl;
import com.sendpo.zjx.sendpo.table.User;
import com.sendpo.zjx.sendpo.utils.Tools;
import com.sendpo.zjx.sendpo.utils.Xutils;

import org.xutils.common.Callback;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by zjx on 2018
 */

public class UserSecuityActivity extends AppCompatActivity {
    public static final int MODFIY_PHONE = 10112;
    public static final int MODIFY_PASS = 10101;
    @BindView(R.id.backbt)
    RelativeLayout backbt;
    @BindView(R.id.head)
    RelativeLayout head;
    @BindView(R.id.iphonevalidate_tv)
    TextView iphonevalidateTv;
    @BindView(R.id.iphonevalidate_iv)
    ImageView iphonevalidateIv;
    @BindView(R.id.iphone_tv)
    TextView iphoneTv;
    @BindView(R.id.iphonevalidate_bt)
    RelativeLayout iphonevalidateBt;
    @BindView(R.id.password_tv)
    TextView passwordTv;
    @BindView(R.id.modifypass_bt)
    RelativeLayout modifypassBt;
    @BindView(R.id.paypassword_tv)
    TextView paypasswordTv;
    @BindView(R.id.paypassword_bt)
    RelativeLayout paypasswordBt;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_usersecuity);
        ButterKnife.bind(this);
        Tools.initStatus(this, R.color.black);
        initComponent();
        locationUserServer();
        webUserServer();
    }

    public void initComponent() {

    }

    @OnClick(value = {R.id.backbt,R.id.iphonevalidate_bt,R.id.modifypass_bt,R.id.paypassword_bt})
    public void onMyClick(View view) {
        switch (view.getId()) {
            case R.id.backbt:
                finish();
                break;
            case R.id.iphonevalidate_bt:
                startActivityForResult(new Intent(this, UsermodifyPhoneActivity.class), 10112);
                break;
            case R.id.modifypass_bt:
                startActivityForResult(new Intent(this, ForgetPassActivity.class), 10101);
                break;
            case R.id.paypassword_bt:
                startActivity(new Intent(this, ModifyPayPassActivity.class));
                break;
        }
    }

    @Override
    protected void onStart() {
        super.onStart();

    }

    public void webUserServer() {
        final List<User> userList = Xutils.queryAll(getApplication(), User.class);
        if (userList == null || userList.size() == 0) {
            return;
        }
        Map<String, String> map = new HashMap<>();
        map.put("userId", userList.get(0).getUserId() + "");
        Xutils.POST(HttpUrl.queryuserbyid, map, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Gson gson = new Gson();
                UserDomain userDomain = gson.fromJson(result, UserDomain.class);
                switch (userDomain.getState()) {
                    case HttpCode.SUCCESS:
                        User user = userDomain.getData();
                        user.setId(userList.get(0).getId());
                        Xutils.update(getApplication(), user);
                        locationUserServer();
                        break;
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    public void locationUserServer()
    {
        List<User> userList = Xutils.queryAll(getApplication(), User.class);
        if(userList==null||userList.size()==0){
            return;
        }
        User user=userList.get(0);
        if(!TextUtils.isEmpty(user.getUserIphone())){
            iphoneTv.setText(""+user.getUserIphone().substring(0,3)+"********");
        }else{
            iphoneTv.setText(""+user.getUserIphone());
        }


    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode!=RESULT_OK){
            return;
        }
        switch (requestCode){
            case MODFIY_PHONE:
                if(data==null){
                    return;
                }
        String str = data.getStringExtra("data");
                iphoneTv.setText(""+str.substring(0,3)+"********");
             break;
            case MODIFY_PASS:
                setResult(RESULT_OK);
                finish();
                break;
        }
    }


}
